package cn.jiyun.controller;

import cn.jiyun.common.R;
import cn.jiyun.entity.Employee;
import cn.jiyun.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmpLoyeeController {
    @Resource
    private EmployeeService employeeService;


    //登陆功能
    @PostMapping("login")
    public R login(HttpServletRequest request, @RequestBody Employee employee){
        String password=employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp=employeeService.getOne(queryWrapper);
        if (emp==null){
            return R.error("登陆失败");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("账号或密码错误");
        }
        if (emp.getStatus()==0){
            return R.error("账号已被冻结");
        }
        //登陆成功后存入session
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("logout")
    public R loOut(HttpServletRequest request){
        //去除掉session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R add(@RequestBody Employee employee,HttpServletRequest request){
        //设置密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置添加时间
        employee.setCreateTime(LocalDateTime.now());
        //设置修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //用户id设置
        //从session中获取
        Long id= (Long) request.getSession().getAttribute("employee");
        //将id传到添加id和修改者的id
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        //进行添加
        employeeService.save(employee);
        return R.success("添加成功");

    }

    //员工信息分页查询用page对象
    //路径
    @GetMapping("page")
    public R<Page> page(int page,int pageSize,String name){
        Page page1=new Page(page,pageSize);//传入对象
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        if (name!=null){
            queryWrapper.like(Employee::getName,name);
        }
        //添加排序
       // Page<Employee> page2 = new Page<>();
        queryWrapper.orderByAsc(Employee::getUpdateTime);
        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }

    @GetMapping("{id}")
    public R<Employee> findById(@PathVariable Long id){
        //查询员工
        Employee employee=employeeService.getById(id);
        return R.success(employee);
    }

    @PutMapping
    public R update(@RequestBody Employee employee,HttpServletRequest request){
        Long id= (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

}
